package com.mindata.blockchain.socket.DGL.listener;

import com.mindata.blockchain.block.block.Block;
import com.mindata.blockchain.core.event.GlobalconcesusEvent;
import com.mindata.blockchain.socket.body.rpc.RpcBlockBody;
import com.mindata.blockchain.socket.body.rpc.RpcSimpleBlockBody;
import com.mindata.blockchain.socket.client.PacketSender;
import com.mindata.blockchain.socket.packet.*;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
public class GlobalconcesusListener {
    @Resource
    private PacketSender packetSender;

    @EventListener
    public void handle(GlobalconcesusEvent globalconcesusEvent) {
        Block block = (Block) globalconcesusEvent.getSource();
        // 标记是全球统一账本共识
        BlockPacket blockPacket = new PacketBuilder<>()
                .setType(PacketType.GLOBALCONCENSUSBEGIN)
                .setBody(new RpcBlockBody(block)).build();
        packetSender.sendToLeaderSet(blockPacket);
    }
}
